package com.kgc.cgprovider.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.basic.util.ResponseMsg;
import com.kgc.cgapi.pojo.*;
import com.kgc.cgprovider.dao.CGXJDao;
import com.kgc.cgprovider.service.CGServ;
import com.kgc.cgprovider.service.CGXJService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class CGXJServiceImpl implements CGXJService {

    private Logger logger = LoggerFactory.getLogger(CGXJServiceImpl.class);

    @Autowired
    private CGXJDao cgxjDao;

    @Resource
    private CGServ cgServ;

    @Override
    public ResponseMsg getCGXJPojo(String code) {
        CGXJPojo cgxjPojo = cgxjDao.getCGXJPojo(code);
        ResponseMsg responseMsg = new ResponseMsg();
        List<CGXJInfo> list = cgxjDao.getCGXJInfo(code);
        cgxjPojo.setList(list);
        responseMsg.setCode(200);
        responseMsg.setData(cgxjPojo);
        return responseMsg;
    }


    @Override
    public ResponseMsg getPage(Map<String, Object> map) {
        ResponseMsg responseMsg = new ResponseMsg();
        int pageNow = 0;
        int pageSize = 0;
        if(map.get("pageNum") instanceof String){
            pageNow =Integer.parseInt((String) map.get("pageNum"));
            pageSize =Integer.parseInt((String) map.get("pageSize"));
        }else {
            pageNow = (Integer) map.get("pageNum");
            pageSize = (Integer) map.get("pageSize");
        }
        PageHelper.startPage(pageNow,pageSize);
        List<CGXJPojo> list = cgxjDao.getCGXJPage(map);
        PageInfo<CGXJPojo> pageInfo = new PageInfo<>(list);
        responseMsg.setCode(200);
        responseMsg.setData(pageInfo);
        return responseMsg;
    }

    @Override
    public ResponseMsg addCGXJ(Map<String, Object> map) {
        logger.info("addCGXJ step 1 map:"+map);
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setCode(500);
        ArrayList<Map<String, Object>> arr =(ArrayList<Map<String, Object>>) map.get("list");
        String code ="CGXJ"+"-"+ cgServ.getCode("cg_bcgxj");
        map.put("code",code);
        responseMsg.setData(code);
        Date date = new Date();
        map.put("create_date",date);
        int result = cgxjDao.addCGXJ(map);
        if (result <= 0){
            return responseMsg;
        }
        for (Map<String,Object> info : arr){
            info.put("major_code",code);
            info.put("create_date",date);
            result = cgxjDao.addCGXJInfo(info);
            if (result <= 0){
                return responseMsg;
            }
        }
        responseMsg.setCode(200);
        return responseMsg;
    }

    @Override
    public ResponseMsg modiCGXJ(Map<String, Object> map) {
        logger.info("modiCGXJ step 1 map:"+map);
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setCode(200);
        String code = (String) map.get("code");
        Date date = new Date();
        ArrayList<Map<String, Object>> arr =(ArrayList<Map<String, Object>>) map.get("list");
        map.put("modi_date" , date);
        int isModi = cgxjDao.modiCGXJ(map);

        List<CGXJInfo> oldList = cgxjDao.getCGXJInfo(code);

        for (Map<String,Object> info : arr){
            String major_code = (String) info.get("major_code");
            if (StringUtils.isBlank(major_code)){
                logger.info("enterAdd=====");
                info.put("major_code",code);
                int result = cgxjDao.addCGXJInfo(info);
                if (result <= 0){
                    responseMsg.setCode(500);
                }
            }else {
                int result = cgxjDao.modiCGXJInfo(info);
                if (result<=0){
                    responseMsg.setCode(500);
                }
                for (int i = 0;i< oldList.size() ; i++){
                    if ((Integer) info.get("id") == oldList.get(i).getId()){
                        oldList.remove(i);
                        break;
                    }
                }
            }
        }
        for (CGXJInfo info : oldList){
            boolean isDel = cgServ.delInfo("cg_bcgxj_info" ,  info.getId());
            if (!isDel){
                responseMsg.setCode(500);
            }
        }
        return responseMsg;
    }

    @Override
    public ResponseMsg importCGXJ(Map<String, Object> map) {
        Map<String, Object> pojo = (Map<String, Object>) map.get("pojo");
        ArrayList<Map<String, Object>> infoList =(ArrayList<Map<String, Object>>) map.get("list");
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setCode(500);
        int result = cgxjDao.importCGXJ(pojo);
        if (result <= 0){
            return responseMsg;
        }
        for (Map<String , Object> info : infoList){
            result = cgxjDao.importCGXJInfo(info);
            if (result <= 0){
                responseMsg.setCode(500);
                return responseMsg;
            }
        }
        responseMsg.setCode(200);
        return responseMsg;
    }

}
